package com.haixiaoke.saas.order.state.role;

import cn.hutool.core.collection.CollectionUtil;
import com.haixiaoke.saas.common.constant.LockConstants;
import com.haixiaoke.saas.common.enums.OrderStateEnum;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.lock.domain.HouseLock;
import com.haixiaoke.saas.lock.domain.LockFace;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.state.server.IOrderStateServerRole;

import java.util.List;

/**
 * 代入住状态服务实现类
 *
 * @author panda
 * @date 2024/4/24
 **/
public class StayCheckInOrderStateServerRole extends AbstractSpecificOrderOrderStateServerRole {

    public StayCheckInOrderStateServerRole(IOrderStateServerRole stateServerRole) {
        super(stateServerRole);
    }

    @Override
    public int cancelReservation(Order order, String recordSource, String recordReason) {
        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_UNSUBSCRIBING.getValue(), recordSource, recordReason, order.getOperatorId());
        order.setOrderState(OrderStateEnum.ORDER_STATE_UNSUBSCRIBING.getValue());
        return orderService.updateOrderBySystem(order);
    }

    @Override
    public int orderCheckIn(Order order, String recordSource, Long userId) {
        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_CHECK_IN.getValue(), recordSource, userId);
        order.setOrderState(OrderStateEnum.ORDER_STATE_CHECK_IN.getValue());

        // 删除房管员人脸授权信息
        List<LockFace> lockFaceList = lockFaceService.selectLockFaceListAndHouseIdAndUserTypeAndFaceState(order.getHouseId(), LockConstants.USER_TYPE_MANAGER, LockConstants.FACE_STATE_ENABLED);
        if (CollectionUtil.isNotEmpty(lockFaceList)) {
            for (LockFace lockFace : lockFaceList) {
                HouseLock houseLock = new HouseLock();
                houseLock.setMac(lockFace.getMac());
                houseLockService.disableLockFaceAndFaceState(houseLock, lockFace);
            }
        }

        return orderService.updateOrderBySystem(order);
    }
}
